package FingerGame;
import java.util.Scanner;

public class Game {

    Person personPlayer;        // 人类玩家
    Computer computerPlayer;    // 电脑玩家
    int fightCount = 0;         // 对战次数

    //初始化游戏，找2个参与游戏的角色
    public void initGame(){
        //创建2个玩家对象
        //确定人类玩家的角色
        //确定电脑玩家的角色
        System.out.println("-----------------");
        System.out.println("---猜拳游戏开始----");
        System.out.println("---1.选择角色-----");
        System.out.println("-----------------");
        personPlayer=new Person();
        String enemyModel = personPlayer.selectEnemyModel();
        computerPlayer=new Computer(enemyModel, 0);
    }

    void start() {
        boolean flag = true;
        // 1-初始化游戏，选择游戏双方的角色
        this.initGame();// this可以省略
        do {
            // 2-对战一局
            this.fight();
            // 是否继续
            System.out.println("已经进行  " + fightCount + "  局对战");
            System.out.println("是否进行下一局,任意键继续，n/N停止。");
            Scanner in = new Scanner(System.in);
            String answer = in.next();
            if (answer.equals("n") || "N".equals(answer)) {
                flag = false;
            }
        } while (flag);

        // 3-显示对战结果
        System.out.println("------------------对战结果------------------");
        System.out.println("玩家1：" + personPlayer.getName() + "\t\t\t积分："
                + personPlayer.getScore());
        System.out.println("玩家2：" + computerPlayer.getModel() + "\t\t\t积分："
                + computerPlayer.getScore());
        System.out.println("------------------------------------------ ");
        if (personPlayer.getScore() != computerPlayer.getScore()) {
            System.out.println("恭喜   " + (personPlayer.getScore()>computerPlayer
                    .getScore() ? personPlayer.getName() : computerPlayer.getModel())
                    + "  胜利");
        } else {
            System.out.println(personPlayer.getName() + " 与 " + computerPlayer.getModel() + "平局。");
        }

    }

    private void fight() {
        System.out.println("---------开始对战---------");
        // 双方出拳
        personPlayer.throwFinger();
        computerPlayer.throwFinger();

        // 处理交换出拳技能
        handleAllSkills();

        // 更新最终出拳结果
        int perNo = personPlayer.getCurrentFingerNo();
        int comNo = computerPlayer.getCurrentFingerNo();

        // 计算结果
        if (perNo == comNo) {
            // 平局处理
            boolean personSkill = personPlayer.getSkills().contains("平局时可视为胜利");
            boolean computerSkill = computerPlayer.getSkills().contains("平局时可视为胜利");

            if (personSkill || computerSkill) {
                if (personSkill) {
                    System.out.println(personPlayer.getName() + " 发动技能 真银斩，平局视为胜利！");
                    personPlayer.win();
                }
                if (computerSkill) {
                    System.out.println(computerPlayer.getModel() + " 发动技能 真银斩，平局视为胜利！");
                    computerPlayer.win();
                }
            } else {
                System.out.println(personPlayer.getName() + " 和 " + computerPlayer.getModel() + "旗鼓相当");
            }
        } else {
            boolean computerWins = isComputerWinOrEqual(comNo, perNo);
            if (computerWins) {
                computerPlayer.win();
            } else {
                personPlayer.win();
            }
        }

        // 处理1％得到1积分的技能
        if (personPlayer.getSkills().contains("卖萌")) {
            if (Math.random() < 0.01) {
                personPlayer.setScore(personPlayer.getScore() + 1);
                System.out.println(personPlayer.getName() + " 发动技能 卖萌，幸运地获得了1分！当前积分：" + personPlayer.getScore());
            }
        }

        fightCount++;
    }

    private boolean isComputerWinOrEqual(int currentCom, int currentPer) {
        return  (currentPer == 1 && currentCom == 2) ||
                (currentPer == 2 && currentCom == 3) ||
                (currentPer == 3 && currentCom == 1) ||
                (currentPer == currentCom);
    }

    private boolean swapFinger(Person personPlayer, Computer computerPlayer) {
        if (Math.random() < 0.5) {
            int temp = personPlayer.getCurrentFingerNo();
            personPlayer.setCurrentFingerNo(computerPlayer.getCurrentFingerNo());
            computerPlayer.setCurrentFingerNo(temp);
            return true;
        }

        return false;
    }

    private void handleAllSkills() {
        boolean personHasSkill = personPlayer.getSkills().contains("ALL之力");
        boolean computerHasSkill = computerPlayer.getSkills().contains("ALL之力");

        if (personHasSkill) handlePlayAllSkill();
        if (computerHasSkill) handleComputerAllSkill();
    }

    private void handlePlayAllSkill() {
        System.out.print("是否发动ALL之力？(Y/N): ");
        String choice = new Scanner(System.in).next().trim().toUpperCase();
        if (choice.equals("Y")) {
            boolean success = swapFinger(personPlayer, computerPlayer);
            System.out.println(personPlayer.getName() + (success ? " 发动ALL之力成功，交换双方出拳！" : " 发动ALL之力失败。"));
        }
    }

    private void handleComputerAllSkill() {
        boolean shouldSwap = !isComputerWinOrEqual(computerPlayer.getCurrentFingerNo(),
                personPlayer.getCurrentFingerNo());
        if (shouldSwap) {
            System.out.println(computerPlayer.getModel() + " 发动ALL之力！");
            boolean success = swapFinger(personPlayer, computerPlayer);
            System.out.println(computerPlayer.getModel() + (success ? " 发动ALL之力成功，交换双方出拳！" : " 发动ALL之力失败。"));
        }
    }

    public static void main(String[] args) {
        new Game().start();
    }
}
